import { API_LIST } from '../../../Api/Api'
import { commonTableList, BuiltInData } from '../Common/common'
import {
    AddDataButton,
    ModDataButton,
    DelDataButton,
    OpenTwoRouter,
    ExamineButton,
    SaveExcelButton,
    SelectExcelButton,
    CommonButton,
} from '../Common/options'
import { SecurityProblemTypeTree } from '../Common/Conversion'
import { ProjectList } from '../Common/Conversion'

import { Badge } from 'antd'

const optionsButton = ['yaxue.ljkjkf.com']
const gjButton = ['jtgjadmin.ljkjkf.com']

const independenceList = {
    'jtgjadmin.ljkjkf.com': 'organizationUuid',
    'jtgjadmin.hljsjtgj.com': 'organizationUuid',
}

const colorMapping = {
    0: '#FA5151',
    1: '#FFC300',
    2: '#00CBC1',
    3: '#236EE7',
    4: '#07C160',
    '-1': 'red',
}

export const Security = async ({ url, OptionsEvent, ClientData, Permission, actionRef }) => {
    const { hostname } = window.location
    const securityProblemTypeUuid = await SecurityProblemTypeTree()
    Permission = Permission.find(v => v.targetUrl === url).children
    const { userUuid } = JSON.parse(sessionStorage.getItem('UserInfo'))

    const optionsData = {
        title: '操作',
        valueType: 'systemOperation',
        fixed: 'right',
        width: 320,
        hideInSearch: true,
        hideInAdded: true,
        hideInModify: true,
        render: (_, record) => {
            const { uuid, auditStatus, createUserUuid } = record
            return [
                Permission.findIndex(v => v.uuid === 'bfd0660c-ef93-4734-aaca-e410b048f3f7') !== -1 && (
                    <ModDataButton OptionsEvent={OptionsEvent} key="modify" title="修改" url={url} data={record} />
                ),
                <OpenTwoRouter
                    key="TwoOne"
                    title="巡检详情"
                    twoTitle="安全巡检问题详情"
                    modal="TableList"
                    OptionsEvent={OptionsEvent}
                    other={{
                        page: 'InspectionProblem',
                        actionRef,
                        Permission,
                        oneData: record,
                        header: data.columns,
                        type: 'security',
                    }}
                />,

                auditStatus !== -1 && auditStatus !== 0 && (
                    <OpenTwoRouter
                        key="TwoTwo"
                        title="整改详情"
                        twoTitle="安全巡检整改详情"
                        modal="TableList"
                        OptionsEvent={OptionsEvent}
                        other={{
                            page: 'InspectionDetails',
                            actionRef,
                            Permission,
                            oneData: record,
                            header: data.columns,
                            type: 'security',
                        }}
                    />
                ),

                createUserUuid === userUuid &&
                    auditStatus === -1 &&
                    Permission.findIndex(v => v.uuid === '85f53e20-49e6-4227-af4b-a3e00b0a4573') !== -1 && (
                        <ExamineButton
                            key="one"
                            title="提交问题"
                            data={{
                                actionRef,
                                OptionsEvent,
                                url: 'patSecuritySubmit',
                                smallTitle: '提交安全巡检',
                            }}
                            options={[
                                { title: '提交', type: 'primary', emit: { path: [uuid] } },
                                { title: '取消', type: 'link', emit: 'off' },
                            ]}
                        />
                    ),
                auditStatus === 0 &&
                    Permission.findIndex(v => v.uuid === '6e46f4c4-f844-4094-8b0a-bcde0879a256') !== -1 && (
                        <ExamineButton
                            key="one"
                            title="接受整改"
                            data={{
                                actionRef,
                                OptionsEvent,
                                url: 'patSecurityAccept',
                                smallTitle: '接受安全巡检整改',
                            }}
                            options={[
                                { title: '接受', type: 'primary', emit: { path: [uuid] } },
                                { title: '取消', type: 'link', emit: 'off' },
                            ]}
                        />
                    ),
                auditStatus === 4 &&
                    Permission.findIndex(v => v.uuid === '6e46f4c4-f844-4094-8b0a-bcde0879a256') !== -1 && (
                        <ExamineButton
                            key="one"
                            title="重新整改"
                            data={{
                                actionRef,
                                OptionsEvent,
                                url: 'patSecurityAccept',
                                smallTitle: '重新接受安全巡检整改',
                            }}
                            options={[
                                { title: '重新接受', type: 'primary', emit: { path: [uuid] } },
                                { title: '取消', type: 'link', emit: 'off' },
                            ]}
                        />
                    ),
                auditStatus === 2 &&
                    Permission.findIndex(v => v.uuid === 'ae37880f-854d-4d77-ab51-40a2c9017512') !== -1 && (
                        <ExamineButton
                            key="one"
                            title="审核整改"
                            data={{
                                actionRef,
                                OptionsEvent,
                                url: 'patSecurityApproval',
                                smallTitle: '审核已整改的安全巡检',
                            }}
                            options={[
                                { title: '合格', type: 'primary', emit: { path: [uuid, 3] } },
                                { title: '不合格', type: 'link', danger: true, emit: { path: [uuid, 4] } },
                            ]}
                        />
                    ),
                Permission.findIndex(v => v.uuid === '3a93ff0e-cf84-44a2-ab45-26417c83fdb6') !== -1 && (
                    <DelDataButton OptionsEvent={OptionsEvent} key="del" title="删除" url={url} uuid={record.uuid} />
                ),
                // 监理回执单
                optionsButton.includes(hostname) && (
                    <SaveExcelButton
                        key="oneOne"
                        title="监理回执单"
                        // OptionsEvent={OptionsEvent}
                        // data={{
                        //     save: true,
                        //     Uuid: uuid,
                        //     url: 'getSecurityGetSupervisionReceipt',
                        // }}
                        api={API_LIST()['getSecurityGetSupervisionReceipt']}
                        data={{ uuid }}
                    />
                ),
                // 监理通知单
                optionsButton.includes(hostname) && (
                    <SaveExcelButton
                        key="oneTwo"
                        title="监理通知单"
                        // OptionsEvent={OptionsEvent}
                        // data={{
                        //     save: true,
                        //     Uuid: uuid,
                        //     url: 'getSecurityGetSupervisionWotice',
                        // }}
                        api={API_LIST()['getSecurityGetSupervisionWotice']}
                        data={{ uuid }}
                    />
                ),
                gjButton.includes(hostname) && (
                    <SaveExcelButton
                        key="G01"
                        title="导出整改通知单"
                        api={API_LIST()['getSecurityWordByUuid']}
                        data={{ uuid }}
                    />
                ),
                gjButton.includes(hostname) && (
                    <SaveExcelButton
                        key="G02"
                        title="导出整改反馈单"
                        api={API_LIST()['getSecurityDetailWordByUuid']}
                        data={{ uuid }}
                    />
                ),
                <CommonButton
                    key="qrcode"
                    OptionsEvent={OptionsEvent}
                    title="整改进度"
                    data={{ type: 'SecurityProcess', uuid }}
                />,
                <CommonButton
                    key="A0001"
                    OptionsEvent={OptionsEvent}
                    title="整改详情"
                    data={{ type: 'SecurityNewDetails', width: '80vw', uuid }}
                />,
            ]
        },
    }

    const gjColumns = [
        commonTableList.uuid,
        { ...commonTableList.SearchSort, selectType: 'Security' },
        commonTableList.BeginSecurityDate,
        commonTableList.EndSecurityDate,
        {
            ...commonTableList.projectUuid,
            valueEnum: await ProjectList(),
            hideInAdded: true,
            hideInModify: true,
            hideInTable: true,
            hideInSearch: !hostname.includes('jszx'),
        },
        {
            dataIndex: 'problem',
            title: '隐患名称',
            valueType: 'input',
            rules: true,
            ellipsis: true,
            width: 400,
            hideInSearch: true,
        },
        {
            dataIndex: 'securityDate',
            title: '填报日期',
            valueType: 'Date',
            showTime: true,
            rules: true,
            width: 150,
            hideInSearch: true,
        },
        { ...commonTableList.tableName, title: '隐患位置', width: 250 },
        {
            dataIndex: 'securityProblemTypeUuid',
            title: '隐患等级',
            valueType: 'treeSelect',
            fieldProps: {
                options: securityProblemTypeUuid,
                fieldNames: { label: 'tableName', value: 'uuid' },
            },
            rules: true,
            ellipsis: true,
            hideInSearch: true,
            width: 250,
        },
        {
            dataIndex: 'limitDays',
            title: '治理时限(天)',
            valueType: 'inputNumber',
            min: 0,
            rules: true,
            width: 120,
            hideInSearch: true,
            render: (_, e) => <div>{e.limitDays === 0 ? '立即整改' : e.limitDays}</div>,
        },
        {
            ...commonTableList.organizationUuid,
            valueEnum: ClientData.organization,
            title: '单位',
        },
        {
            dataIndex: 'responsiblePerson',
            title: '整改责任人',
            valueType: 'selectArr',
            getUrl: 'getResponsiblePerson',
            getFiles: 'organizationUuid',
            rules: true,
            valueEnum: {},
            width: 100,
            hideInSearch: true,
            hideInTable: true,
        },
        {
            dataIndex: 'acceptancePerson',
            title: '验收人',
            valueType: 'selectArr',
            getUrl: 'getSuperiorOrResponsiblePerson',
            getFiles: 'organizationUuid',
            valueEnum: {},
            hideInSearch: true,
            hideInTable: true,
        },

        {
            dataIndex: 'responsiblePersonName',
            title: '整改责任人',
            valueType: 'input',
            width: 120,
            hideInSearch: true,
            hideInAdded: true,
            hideInModify: true,
        },
        {
            dataIndex: 'acceptancePersonName',
            title: '验收人',
            valueType: 'input',
            width: 120,
            hideInSearch: true,
            hideInAdded: true,
            hideInModify: true,
        },
        {
            dataIndex: 'responsibleDepartment',
            title: '责任单位',
            valueType: 'input',
            width: 100,
            hideInSearch: true,
        },
        {
            dataIndex: 'reCheckResult',
            title: '复查结果',
            valueType: 'input',
            width: 100,
            hideInSearch: true,
        },
        {
            dataIndex: 'lng',
            title: '经度',
            valueType: 'input',
            width: 100,
            hideInSearch: true,
        },
        {
            dataIndex: 'lat',
            title: '纬度',
            valueType: 'input',
            width: 100,
            hideInSearch: true,
        },
        {
            dataIndex: 'auditStatus',
            title: '状态',
            valueType: 'select',
            valueEnum: BuiltInData.PatrolStatus,
            hideInAdded: true,
            hideInModify: true,
            width: 150,
            render: (_, e) => (
                <Badge color={colorMapping[e.auditStatus]} text={BuiltInData.PatrolStatus[e.auditStatus]} />
            ),

            // <div style={{ color: colorMapping[e.auditStatus] }}>{BuiltInData.PatrolStatus[e.auditStatus]}</div>
        },

        { ...commonTableList.createUserName, title: '下单人' },
        commonTableList.createTime,
        commonTableList.updateUserName,
        commonTableList.updateTime,
        optionsData,
    ]

    let data = {
        pageSize: 10,
        params: { pageIndex: 1, OSecurityDate: 'desc@1' },

        independenceSet: {
            title: '组织机构',
            url: 'getOrgTree',
            data: { IsByUserUuid: 1 },
            type: 'TableNavTree001',
            all: true,
        },
        independence: independenceList[hostname],
        get: API_LIST()[`get${url}`],
        added: `pos${url}`,
        modify: `put${url}`,
        delete: `del${url}`,
        options: e => [
            Permission.findIndex(v => v.uuid === 'f43233b0-e329-4644-96d7-07e3670216c0') !== -1 && (
                <AddDataButton OptionsEvent={OptionsEvent} url={url} type="primary" icon="add" title="新增巡检" />
            ),
            e && (
                <SelectExcelButton
                    key="excel"
                    title="导出记录"
                    OptionsEvent={OptionsEvent}
                    api={API_LIST()['getSecurityExcel']}
                    data={{ organizationUuid: e }}
                />
            ),
        ],
        columns: gjColumns,
    }
    return data
}
